package com.ruicar.afs.cloud.afscase.approvetask.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.afscase.approveocr.entity.ApproveOcrBatch;
import com.ruicar.afs.cloud.afscase.approvetask.entity.CaseApproveRecord;
import com.ruicar.afs.cloud.afscase.approvetask.entity.WorkTaskPool;
import com.ruicar.afs.cloud.afscase.approvetask.mapper.CaseApproveRecordMapper;
import com.ruicar.afs.cloud.afscase.approvetask.service.CaseApproveRecordService;
import com.ruicar.afs.cloud.afscase.approvetask.vo.ApproveRecordVO;
import com.ruicar.afs.cloud.afscase.processor.enums.NormalSubmitType;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.uid.AfsSequenceGenerator;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.ApproveTypeEnum;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.FlowNodeEnum;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.UseSceneEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * <p>Description: </p>
 *
 * @author ZC.GUO
 * @version 1.0
 * @date create on 2020-05-13 15:20
 */
@Service
@AllArgsConstructor
@Data
@Slf4j
public class CaseApproveRecordServiceImpl extends ServiceImpl<CaseApproveRecordMapper, CaseApproveRecord> implements CaseApproveRecordService {

    private AfsSequenceGenerator afsSequenceGenerator;
    private static final String SEQ_TYPE_RECORD = "SEQ_TYPE_CASE_RECORD";
    private static final String ID_START = "9";

    @Override
    public boolean save(CaseApproveRecord record){
        String prefix = DateUtil.format(new Date(),"yyyyMMdd");
        Long seq = afsSequenceGenerator.genNext(SEQ_TYPE_RECORD,prefix);
        String padSeq = StringUtils.leftPad(Convert.toStr(seq),10,"0"); // 10位
        String padPrefix = StringUtils.leftPad(prefix+padSeq,18,"0"); // 18位
        record.setId(Convert.toLong(ID_START + padPrefix));

        // 使用场景添加默认值，如果没有设置，则默认信审
        if(StringUtils.isBlank(record.getUseScene())){
            record.setUseScene(UseSceneEnum.APPROVE.getValue());
        }
        return super.save(record);
    }

    @Override
    public void ocrSendBack(ApproveOcrBatch ocrBatch){
        CaseApproveRecord record = new CaseApproveRecord();
        record.setApproveSuggest(AfsEnumUtil.key(NormalSubmitType.SEND_BACK_TO_DEALER));
        record.setApproveSuggestName(AfsEnumUtil.desc(NormalSubmitType.SEND_BACK_TO_DEALER));
        record.setDisposeStaff(SecurityUtils.getUser().getUserRealName());
        record.setApplyNo(ocrBatch.getApplyNo());
        record.setApproveRemark(ocrBatch.getRemark());
        record.setUseScene(UseSceneEnum.APPROVE.getValue());
        record.setApproveStartTime(ocrBatch.getCreateTime());
        record.setApproveEndTime(new Date());
        record.setApproveType(ApproveTypeEnum.REMIND.getValue());
        record.setDisposeNodeName("人工签名确认");
        save(record);
    }

    @Override
    public void repeatIntoOcr(ApproveOcrBatch ocrBatch) {
        CaseApproveRecord record = new ApproveRecordVO();
        record.setApplyNo(ocrBatch.getApplyNo());
        record.setApproveSuggestName("提交");
        record.setDisposeNodeName("签名确认退回后提交");
        record.setDisposeStaff("系统");
        record.setUseScene(UseSceneEnum.APPROVE.getValue());
        record.setApproveStartTime(ocrBatch.getCreateTime());
        record.setApproveEndTime(new Date());
        record.setApproveType(ApproveTypeEnum.REMIND.getValue());
        save(record);
    }

    @Override
    public void appoint(String applyNo){
        CaseApproveRecord record = new CaseApproveRecord();
        record.setApplyNo(applyNo);
        record.setUseScene(UseSceneEnum.APPROVE.getValue());
        record.setDisposeStaff(SecurityUtils.getUser().getUserRealName());
        record.setDisposeNodeName("指定处理人");
        record.setApproveSuggestName("指定处理人");
        record.setApproveStartTime(new Date());
        record.setApproveEndTime(new Date());
        record.setApproveRemark("发起指定处理人流程");
        save(record);
    }

    @Override
    public void taskArrival(WorkTaskPool pool) {
        CaseApproveRecord record = new CaseApproveRecord();
        record.setApplyNo(pool.getApplyNo());
        record.setStageId(pool.getStageId());
        record.setUseScene(UseSceneEnum.APPROVE.getValue());
        record.setDisposeStaff(pool.getApproveStaffName());
        record.setDisposeNodeName(pool.getTaskNodeName());
        record.setDisposeNode(pool.getTaskNodeId());
        record.setApproveStartTime(new Date());
        save(record);
    }

    @Override
    public void repeal(String applyNo,String msg){
        CaseApproveRecord record = new CaseApproveRecord();
        record.setApplyNo(applyNo);
        record.setApproveSuggestName("撤销");
        record.setUseScene(UseSceneEnum.APPROVE.getValue());
        record.setDisposeStaff(Objects.nonNull(SecurityUtils.getUser())? SecurityUtils.getUser().getUserRealName() : "系统");
        record.setApproveStartTime(new Date());
        record.setApproveEndTime(new Date());
        save(record);
    }


    public void assignRecord(WorkTaskPool pool){
        CaseApproveRecord caseApproveRecord=new CaseApproveRecord();
        caseApproveRecord.setFlowNode(FlowNodeEnum.ASSIGN.getCode());
        caseApproveRecord.setStageId(pool.getStageId());
        caseApproveRecord.setDisposeStaff(pool.getApproveStaffName());
        caseApproveRecord.setApplyNo(pool.getApplyNo());
        caseApproveRecord.setContractNo(pool.getContractNo());
        caseApproveRecord.setApproveStartTime(new Date());
        caseApproveRecord.setUseScene(pool.getProcessType()==null?UseSceneEnum.GENERAL_LOAN.getValue():pool.getProcessType());
        caseApproveRecord.setDisposeNodeName(pool.getTaskNodeName());
        caseApproveRecord.setDisposeNode(pool.getTaskNodeId());
        caseApproveRecord.setTaskId(pool.getId().toString());
        save(caseApproveRecord);
    }

}
